package com.blog.back.service.impl;

import com.blog.back.redis.RedisUtils;
import com.blog.utils.PageUtils;
import com.blog.utils.Query;
import com.blog.utils.RedisKeys;
import com.blog.vo.UsersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blog.back.dao.UsersDao;
import com.blog.entity.UsersEntity;
import com.blog.back.service.UsersService;
import org.springframework.util.StringUtils;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {


    @Autowired
    RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UsersEntity> page = this.page(
                new Query<UsersEntity>().getPage(params),
                new QueryWrapper<UsersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UsersVO getUser(Long userId) {

        Object obj = redisUtils.get(RedisKeys.getUserKey(userId));
        if (!StringUtils.isEmpty(obj)) {
            return (UsersVO) obj;
        }

        UsersEntity usersEntity = getBaseMapper().selectById(userId);
        UsersVO usersVO = new UsersVO(usersEntity);
        if (!StringUtils.isEmpty(usersVO))
            redisUtils.set(RedisKeys.getUserKey(userId), usersVO, 60 * 60 * 24 * 7);
        return usersVO;
    }
}